package com.celebriosoftware.client.receiver;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.support.v4.app.Fragment;
import android.util.Log;

import com.celebriosoftware.client.fragments.DesktopFragment;

public class WifiReceiver extends BroadcastReceiver {

	private int wifiState = 3;
	private DesktopFragment fragment = null;
	
	public WifiReceiver() {
	}
	public WifiReceiver(DesktopFragment fragment) {
		this.fragment=fragment;
	}
	
	@Override
	public void onReceive(Context context, Intent intent) {

		WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
		WifiInfo info = wifiManager.getConnectionInfo();
		wifiState = WifiManager.calculateSignalLevel(info.getRssi(), 4);
		reportWifiState();
	}
	private void reportWifiState() {
		if(fragment != null) {
			fragment.updateWifiState(wifiState);
		}
		else {
			Log.w("wifi receiver", "parent activity not supplied");
		}
	}
	
	public int getWifiState(){
		return wifiState;
	}
}
